package com.xiaoyu.fish.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.*;
import com.xiaoyu.fish.common.Result;
import com.xiaoyu.fish.dto.LocationDto;
import com.xiaoyu.fish.pojo.Callout;
import com.xiaoyu.fish.pojo.Location;
import com.xiaoyu.fish.pojo.Position;
import com.xiaoyu.fish.service.impl.PositioningServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/locate")
public class PositioningController {



    @Autowired
    private PositioningServiceImpl positioningService;
    String ak = "28aCKqynTc2AvOGCCriv0lS1XsYIXsC5";


    /**
     * 查询所有的position，用于网页显示，和小程序渔网总数
     *
     * @return
     */
    @GetMapping("/list")
    public Result selectAllPoint() {
//        从数据库中查询
        List<Position> positionList = positioningService.listPosition();
        return Result.success(positionList);
    }

    /**
     * 根据id查询单个
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public Result selectAndDeleteOne(@PathVariable int id) {
//        从数据库中查询
        Position position = positioningService.selectOne(id);
        System.out.println("查询单个position" + position);
        return Result.success(position);
    }

    /**
     * 根据id查询单个,用于渔网选择
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectOne(@PathVariable int id) {
//        从数据库中查询
        Position position = positioningService.selectOne(id);
        System.out.println("查询单个position" + position);
        Callout callout = new Callout();
        System.out.println("测试callout");
        System.out.println(callout);
        callout.setContent("第" + position.getIndex_net().toString() + "排");
        Location startLocation = new Location();
        startLocation.setLatitude(position.getStart_latitude().toString());
        startLocation.setLongitude(position.getStart_longitude().toString());
        startLocation.setCallout(callout);
        startLocation.setId(0);

        Location stopLocation = new Location();
        stopLocation.setCallout(callout);
        stopLocation.setLatitude(position.getStop_latitude().toString());
        stopLocation.setLongitude(position.getStop_longitude().toString());
        startLocation.setId(1);

        List<Location> locations = new ArrayList<>();
        locations.add(startLocation);
        locations.add(stopLocation);
        System.out.println(locations);
        return Result.success(locations);
    }

    /**
     * 根据id删除position
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable int id) {
//        System.out.println("执行删除！");
        String result = positioningService.deleteById(id);
        System.out.println(result);
        return Result.success(result);
    }

    /**
     * 小程序传递过来的是gcj02坐标体系，注意转换
     *
     * @param position
     * @return
     */
    @PostMapping("/save")
    public Result savePoint(@RequestBody Position position) {
//        写入数据库
        System.out.println(position);
        String stringResult = positioningService.savePoint(position);
        return Result.success(stringResult);
    }

    /**
     * 坐标转换服务
     *
     * @param location
     */
    private void addTrack(Location location) {
        String urlConvert = "https://api.map.baidu.com/geoconv/v1/?coords={},{}&from=1&to=5&ak={}";
        String formatUrl = StrUtil.format(urlConvert, location.getLongitude(), location.getLatitude(), ak);
        System.out.println(formatUrl);
        String body1 = HttpRequest.get(formatUrl).execute().body();
        System.out.println(body1);
        JSONObject jsonObject = JSONUtil.parseObj(body1);
        JSONArray resultArray = jsonObject.getJSONArray("result");
        JSONObject jsonObject1 = resultArray.getJSONObject(0);
        String x = jsonObject1.getStr("x");
        String y = jsonObject1.getStr("y");
        System.out.println("转换后的经度" + x + "," + "转换后的纬度" + y);
    }


}
